package com.gopay.bis.manager.bank.ebank;

import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.abc.ebank.AbcB2cMessage;
import com.gopay.common.domain.user.mcc.UserMccTransformReq;
import com.gopay.common.domain.user.mcc.UserMccTransformRes;
import com.gopay.common.exception.GopayException;
import com.gopay.common.user.mcc.manager.UserMccTransformManager;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 农行网关B2C对象封装
 * 
 * @author AbcB2cManager.java
 * @author LiLiLi
 */
public class AbcB2cManager implements BankManagerRemoteService {
    
    @Resource(name = "abcB2cBankServiceClient")
    private BankService abcB2cBankServiceClient;
    
    @Resource(name = "abcB2cBankServiceClientHna")
    private BankService abcB2cBankServiceClientHna;
    
    @Resource(name = "abcConfig")
	private Map<String, String> abcConfig;
    
    @Resource(name = "userMccTransformManager")
	private UserMccTransformManager userMccTransformManager;
    
    
    /**
	 * B2C签名
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
    public Object sign(BaseMessage message) throws GopayException {
    	UserMccTransformReq userM = new UserMccTransformReq();
		userM.setBankCode(BankConstants.BANK_LOGO_ABC);
		userM.setCustId(message.getSpecial2());
		UserMccTransformRes userRes = userMccTransformManager.graspUserMccInfo(userM);
    	AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		Date orderDate = DateUtils.strToDate(messageB2C.getOrderDate());
		messageB2C.setTrnxDate(DateUtils.format(orderDate, "yyyy/MM/dd"));
		messageB2C.setTrnxTime(DateUtils.format(orderDate, "HH:mm:ss"));
		
		messageB2C.setResultNotifyURL(messageB2C.getPayRuturnUrl());
		if (userRes == null) {
			if ((BankConstants.GOPAY_CHONGZHI_CUSTID).equals(message.getSpecial2())) {
				messageB2C.setSubMerId(BankConstants.GOPAY_CODE);
				messageB2C.setSubMerName(BankConstants.GOPAY_NAME);
				messageB2C.setSubMerMCC(messageB2C.getSubMerId().substring(4, 8));
				messageB2C.setSubMerchantRemarks("国付宝充值");
			}
		} else {
			//p2p商户上送mcc处理
			if(userRes.getGopayMccCode().equals(BankConstants.GOPAY_MCC_CODE)){
				String gopayMccCode = (String) abcConfig.get("ABC.B2C.GOPAY.MCC.CUSTID");
		      	String [] gopayMccCodeArray = gopayMccCode.split(",");
		      	Random rand = new Random();		
		      	int num = rand.nextInt(gopayMccCodeArray.length);
		      	String custId = new String(gopayMccCodeArray[num]);
		      	UserMccTransformReq userReq = new UserMccTransformReq();
		      	userReq.setBankCode(BankConstants.BANK_LOGO_ABC);
		      	userReq.setCustId(custId);
		      	messageB2C.setSendBankCustId(custId);
		      	UserMccTransformRes userRess = userMccTransformManager.graspUserMccInfo(userReq);
		      	if(userRess == null){
		      		userReq.setBankCode(BankConstants.BANK_LOGO_ABC);
		      		userReq.setCustId("0000001726");
                    messageB2C.setSendBankCustId("0000001726");
			      	userRess = userMccTransformManager.graspUserMccInfo(userReq);
		      	}
		      	String subPMerId = ""+userRess.getProvince()+""+userRess.getBankMccCode()+""+userRess.getGopayMccNum();
				messageB2C.setSubMerName(userRess.getCorpName());
				messageB2C.setSubMerId(subPMerId);
				messageB2C.setSubMerMCC(userRess.getBankMccCode());
				messageB2C.setSubMerchantRemarks(userRess.getBankMccDesc());
			}else{
				String subMerId = ""+userRes.getProvince()+""+userRes.getBankMccCode()+""+userRes.getGopayMccNum();
				messageB2C.setSubMerName(userRes.getCorpName());
				messageB2C.setSubMerId(subMerId);
				messageB2C.setSubMerMCC(userRes.getBankMccCode());
				messageB2C.setSubMerchantRemarks(userRes.getBankMccDesc());
			}
		}
		
		messageB2C.setMerchantRemarks(messageB2C.getSubMerName()+"GopayB2C");
		// 调用远程接口请求签名数据
		BankLogUtils.printB2CManagerSignSendLog(messageB2C);		 
		if(messageB2C.isHna()){
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClientHna.sign(messageB2C);			
		}else{
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClient.sign(messageB2C);		
		}
		BankLogUtils.printB2CManagerSignReturnLog(messageB2C);
		return messageB2C;
	}
	/**
	 * B2C验签
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
    public Object verify(BaseMessage message) throws GopayException {
		AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		BankLogUtils.printB2CManagerVerifySendLog(messageB2C);
		AbcB2cMessage abcMessageB2C;
		if(messageB2C.isHna()){
			abcMessageB2C = (AbcB2cMessage)abcB2cBankServiceClientHna.verify(messageB2C);			
		}else{
			abcMessageB2C = (AbcB2cMessage)abcB2cBankServiceClient.verify(messageB2C);		
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(messageB2C, abcMessageB2C.isSuccess());
		return abcMessageB2C;
	}

	/**
	 * B2C订单查询
	 * 
     * @param Object
     * @return Object
     * @throws GopayException
	 */
    public Object query(Object message) throws GopayException {
		AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		messageB2C.setQueryDetail("1"); // 1: 详细查询
		BankLogUtils.printB2CManagerQuerySendLog(messageB2C, messageB2C);
		if(messageB2C.isHna()){
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClientHna.query(messageB2C);	
		}else{
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClient.query(messageB2C);	
		} 
		BankLogUtils.printB2CManagerQueryReturnLog(messageB2C);
		return messageB2C;
	}
	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
